/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.comandos.visit;

import br.ufc.quixada.adrs.interfaces.Comando;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.util.Msg;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Welligton
 */
public class CmdVisitMontaRelatorioPassoDois implements Comando {

    public String executa(HttpServletRequest request, HttpServletResponse response) {

        HttpSession sessao = request.getSession(true);
        String retornoAnterior = (String)sessao.getAttribute("retornoAnterior2");
        String retorno = request.getParameter("retorno");
        List<Long> idProd = new ArrayList<Long>();
        boolean hasOneAdrsComboBoxSelected = request.getParameter("cb") != null ? true : false;
        if (hasOneAdrsComboBoxSelected) {
            String arrayAdrsIDComboBox[] = request.getParameterValues("cb");
            for (int i = 0; i < arrayAdrsIDComboBox.length; i++) {
                idProd.add(Long.parseLong(arrayAdrsIDComboBox[i]));
            }
        } else {
            sessao.setAttribute("erro", Msg.EMPTY_SELECTION_ERROR_ADRS);
            return "/visit/"+retornoAnterior+".jsp";
        }
        
        List<Adrs> listaAdrsQS = new ArrayList<Adrs>();
        List<Adrs> listaAdrsSelecionado = (List<Adrs>) sessao.getAttribute("listAdrsQS");
        for (Adrs ad : listaAdrsSelecionado) {
            for (Long id : idProd) {
                if (id.equals(ad.getId())) {
                    listaAdrsQS.add(ad);
                    break;
                }
            }
        }
        sessao.setAttribute("adrsSelecionadoQSanitario", listaAdrsQS);
        sessao.setAttribute("retornoAnterior3", retorno);

        return "/visit/" + retorno + ".jsp";
    }
}
